package com.gooluke.shorten.controller;

import com.gooluke.common.domain.Result;
import com.gooluke.shorten.model.*;
import com.gooluke.shorten.model.vo.CreateShortUrlVO;
import com.gooluke.shorten.service.ShortUrlService;
import io.swagger.annotations.ApiOperation;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/shorten")
public class ShortUrlController {

    @Autowired
    private ShortUrlService shortUrlService;

    @PostMapping("/create")
    @ApiOperation("生成短链")
    public Result<CreateShortUrlVO> createShortUrl(@Valid @RequestBody GenerateRequest request) {
        CreateShortUrlVO shortUrl = shortUrlService.createShortUrl(request);
        return Result.success(shortUrl);
    }


    @PatchMapping("/{code}")
    @ApiOperation("更新短链信息")
    public Result<Void> updateShortUrl(@PathVariable(value = "code") String code,
                                       @Valid @RequestBody UpdateShortUrlRequest request) {
        shortUrlService.updateShortUrl(request);
        return Result.success();
    }

    @PostMapping("/{code}/archive")
    @ApiOperation("归档短链")
    @Transactional
    public Result<Void> archiveShortUrl(@PathVariable(value = "code") String code) {
        shortUrlService.archiveShortUrl(code);
        return Result.success();
    }

    @PostMapping("/batch/delete")
    @ApiOperation("批量删除短链")
    @Transactional
    public Result<Void> batchDelete(@Valid @RequestBody BatchDeleteRequest request) {
        shortUrlService.batchDelete(request);
        return Result.success();
    }

    @GetMapping("/{code}/preview")
    @ApiOperation("获取短链预览信息")
    public Result<UrlPreviewResult> previewShortUrl(@PathVariable(value = "code") String code, @RequestParam(defaultValue = "5000") int timeout) {
        // todo
        return null;
    }

    @PostMapping("/{code}/renew")
    @ApiOperation("短链续期")
    public Result<Void> renewShortUrl(@PathVariable(value = "code") String code, @Valid @RequestBody RenewRequest request) {
        shortUrlService.renewShortUrl(code, request);
        return Result.success();
    }
}
